package com.fj.netty.protocol;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;

/**
 * @author fj
 * @date 2024/1/9 09:56
 */
@Slf4j
public class TestHttpProtocal {
    public static void main(String[] args) {
        NioEventLoopGroup group = new NioEventLoopGroup();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap()
                    .group(group)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {

                            ch.pipeline().addLast(new LoggingHandler());
                            ch.pipeline().addLast(new HttpServerCodec());
                            ch.pipeline().addLast(new SimpleChannelInboundHandler<HttpRequest>() {
                                @Override
                                protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
                                    byte[] content = "<p>hello</p>".getBytes(Charset.defaultCharset());
                                    DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK);
                                    response.headers().add(HttpHeaders.Names.CONTENT_LENGTH,content.length);
                                    response.content().writeBytes(content);
                                    ctx.writeAndFlush(response);
                                }
                            });
//                            ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
//                                @Override
//                                public void channelActive(ChannelHandlerContext ctx) throws Exception {
//                                    log.debug("连接成功");
//                                    super.channelActive(ctx);
//                                }
//
//                                @Override
//                                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
////                                    System.out.println(msg.getClass());
//                                    if (msg instanceof HttpRequest){
//                                        byte[] content = "<p>hello</p>".getBytes(Charset.defaultCharset());
//                                        DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK);
//                                        response.headers().add(HttpHeaders.Names.CONTENT_LENGTH,content.length);
//                                        response.content().writeBytes(content);
//                                        ctx.writeAndFlush(response);
//                                    }else if (msg instanceof HttpContent){
//
//                                    }
//                                    super.channelRead(ctx, msg);
//                                }
//                            });
                        }
                    });
            ChannelFuture channelFuture = serverBootstrap.bind(6789).sync();
            channelFuture.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();
        }
    }
}
